package org.xmh.demo.account.action;

import java.util.Date;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.personal.struts.action.LoginAware;
import org.personal.util.StringUtil;
import org.xmh.demo.entity.User;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 5863123363533372435L;
	private User user;
	private String code;
	private Map<String, Object> session;

	public String register() throws Exception {
		System.out.println("验证通过");
		return SUCCESS;
	}
	
	public User getUser(){
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}


	public void validateRegister(){
		if(user!=null){
			if(StringUtil.isEmptyOrNull(user.getName())){
				addFieldError("name",getText("login.user.name.empty"));
			}else if(user.getName().length()<5){
				addFieldError("name",getText("login.user.name.tooShort"));
			}else if(user.getName().length()>20){
				addFieldError("name",getText("login.user.name.overLong"));
			}
			
			if(StringUtil.isEmptyOrNull(user.getPassword())){
				addFieldError("password",getText("login.user.password.empty"));
			}else if(user.getPassword().length()<6){
				addFieldError("password",getText("login.user.password.tooShort"));
			}else if(user.getPassword().length()>30){
				addFieldError("password",getText("login.user.password.overLong"));
			}
			
			if(user.getBirthday()==null){
				addFieldError("birthday",getText("login.user.birthday.empty"));
			}else if(new Date().before(user.getBirthday())){
				addFieldError("birthday",getText("login.user.birthday.error"));
			}
			
			if(StringUtil.isEmptyOrNull(user.getEmail())){
				addFieldError("email",getText("login.user.email.empty"));
			}else if(StringUtil.isEmail(user.getEmail())){ 
				addFieldError("email",getText("login.user.email.error"));
			}
			
			if(StringUtil.isEmptyOrNull(user.getNationality())){
				addFieldError("nationality",getText("login.user.nationality.empty"));
			}
			
			Object code=session.get(LoginAware.SESSION_SECURITY_CODE);
			
			if(StringUtil.isEmptyOrNull(getCode())){
				addFieldError("code",getText("login.user.code.empty"));
			}else if(!getCode().equals(code)){
				addFieldError("code",getText("login.user.code.error"));
			}
		}
	}

	public void setSession(Map<String, Object> session) {
		this.session=session;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
}
